<template>
  <div id="box">
    <el-card header="请登录">
      <el-form @submit.native.prevent="login">
        <el-form-item label="用户名">
          <el-input v-model="model.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="model.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="loginBtn" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        model: {
          username: '',
          password: ''
        }
      }
    },
    methods: {
      async login() {
        const res = await this.$http.post('/login', this.model);
        // 把服务器返回的 token 记录到 localStorage
        localStorage.token = res.data.token
        this.$router.push('/');
        this.$message({
          type: 'success',
          message: '登录成功'
        })
      }
    }
  }
</script>

<style scoped>
  #box {
    width: 30%;
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  /* .el-card {

  } */
  .loginBtn {
    width: 100%;
  }
</style>